<template>
  <div class="user-container">
    <!-- 用户信息卡片 -->
    <div class="user-card">
      <div class="avatar">
        <van-icon name="user-o" size="50" />
      </div>
      <div class="user-info">
        <div class="username">{{ userInfo.userName }}</div>
        <div class="user-detail">
          <span>性别: {{ userInfo.userSex === 1 ? '男' : '女' }}</span>
          <span>ID: {{ userInfo.userId }}</span>
        </div>
      </div>
    </div>

    <!-- 功能列表 -->
    <div class="function-list">
      <!-- 地址管理 -->
      <div class="function-item" @click="router.push('/address')">
        <div class="item-left">
          <van-icon name="location-o" />
          <span>收货地址</span>
        </div>
        <van-icon name="arrow" />
      </div>
      
      <!-- 其他功能项 -->
      <div class="function-item">
        <div class="item-left">
          <van-icon name="orders-o" />
          <span>我的订单</span>
        </div>
        <van-icon name="arrow" />
      </div>
    </div>

    <!-- 退出登录按钮 -->
    <div class="logout-btn" @click="handleLogout">
      退出登录
    </div>

    <!-- 地址列表弹出层 -->
    <van-popup v-model:show="showAddressPopup" position="bottom" round>
      <div class="address-popup">
        <div class="popup-header">
          <span>收货地址</span>
          <van-icon name="cross" @click="showAddressPopup = false" />
        </div>
        <div class="address-list">
          <div class="address-item" v-for="address in addressList" :key="address.daId">
            <div class="contact-info">
              <span class="name">{{ address.contactName }}</span>
              <span class="gender">{{ address.contactSex === 1 ? '先生' : '女士' }}</span>
              <span class="phone">{{ address.contactTel }}</span>
            </div>
            <div class="address-detail">{{ address.address }}</div>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import request from '@/utils/request'

const router = useRouter()
const userInfo = ref({})
const addressList = ref([])
const showAddressPopup = ref(false)

// 获取用户信息
const getUserInfo = async () => {
  try {
    const userName = JSON.parse(localStorage.getItem('userInfo')).userName
    const res = await request.get(`/user/getUserByName?userName=${userName}`)
    if (res.data.code === 200) {
      userInfo.value = res.data.data
      // 获取用户地址列表
      getAddressList(res.data.data.userId)
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

// 获取地址列表
const getAddressList = async (userId) => {
  try {
    const res = await request.get(`/deliveryAddress/listDeliveryAddressByUserId?userId=${userId}`)
    if (res.data.code === 200) {
      addressList.value = res.data.data
    }
  } catch (error) {
    console.error('获取地址列表失败:', error)
  }
}

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  router.push('/login')
}

onMounted(() => {
  getUserInfo()
})
</script>

<style scoped>
.user-container {
  padding: 20px;
  min-height: 100vh;
  background: #f5f5f5;
}

.user-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.user-info {
  flex: 1;
}

.username {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.user-detail {
  font-size: 14px;
  color: #666;
}

.user-detail span {
  margin-right: 15px;
}

.function-list {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 20px;
}

.function-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f5f5f5;
}

.item-left {
  display: flex;
  align-items: center;
}

.item-left .van-icon {
  font-size: 20px;
  margin-right: 10px;
  color: #0097ff;
}

.logout-btn {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  text-align: center;
  color: #ff4d4f;
  font-size: 16px;
}

.address-popup {
  padding: 20px;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 15px;
  border-bottom: 1px solid #f5f5f5;
  font-size: 16px;
  font-weight: bold;
}

.address-list {
  max-height: 60vh;
  overflow-y: auto;
}

.address-item {
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}

.contact-info {
  margin-bottom: 8px;
}

.contact-info .name {
  font-size: 16px;
  font-weight: bold;
  margin-right: 8px;
}

.contact-info .gender {
  margin-right: 8px;
  color: #666;
}

.contact-info .phone {
  color: #666;
}

.address-detail {
  font-size: 14px;
  color: #333;
}
</style> 